package com.china_goldcard.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonNull;

import java.lang.reflect.Type;

/**
 * Gson工具类
 */
public class GsonUtils {

    public static final String DATE_FORMAT = "yyyyMMddHHmmss";

    private GsonUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static Gson getGson(){
        return new GsonBuilder().serializeNulls().setDateFormat(DATE_FORMAT).create();
    }

    public static String toJsonStr(Object obj){
        if (obj == null){
            return getGson().toJson(JsonNull.INSTANCE);
        }
        return getGson().toJson(obj);
    }

    public static <T> String toJsonStr2(T t){
        return getGson().toJson(t);
    }

    public static <T> T fromJson(String json,Class<T> clz){
        return getGson().fromJson(json,clz);
    }

    public static Object fromJson(String json, Type typeOfT) {
        return getGson().fromJson(json, typeOfT);
    }
}
